package com.yue.excel;

import org.apache.poi.ss.usermodel.*;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author: yyk
 * @Description: 读取excel文件数据
 * @Date: Created in 15:35 2024/8/4
 * @Since version-1.0
 */
public class ReadExcelTest {
    //你的excel文件路径
    String excelFilePath = "D:\\Program Files\\JetBrains\\excel_util\\";

    @Test
    public void  readeExcel() {
        try (FileInputStream inputStream = new FileInputStream(new File(excelFilePath+"2024数据生成.xlsx"))) {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            // 迭代工作表中的行
            for (Row row : sheet) {
                // 迭代行中的单元格
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        case FORMULA:
                            System.out.print(cell.getCellFormula() + "\t");
                            break;
                        case BLANK:
                            System.out.print("[BLANK]\t");
                            break;
                        default:
                            System.out.print("[UNKNOWN]\t");
                    }
                }
                System.out.println(); // 换行
            }

            workbook.close(); // 关闭工作簿

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
